package com.md24housecall.net;

import org.json.JSONObject;

import android.text.TextUtils;

import com.md24housecall.model.Doctor;
import com.md24housecall.model.TagUser;
import com.md24housecall.model.User;
import com.md24housecall.util.GlobalConstant;
import com.md24housecall.util.MyLog;

public class ParsingUtils {

	/** Parse JSON data of TagUser which get from UrlBuilder.buildWSAddUser() */
	public static TagUser parseUser(String html) {
		if(TextUtils.isEmpty(html))
			return null;
		
		try {
			JSONObject json = new JSONObject(html);
			return parseUser(json);
		} catch (Exception e) {
			return null;
		}
	}
	public static TagUser parseUser(JSONObject json) {
		if(json == null)
			return null;
		
		TagUser user = null;
		try {
			user = new TagUser();
			user._ID = json.getString("id");
			user._user_login = json.getString("username");
			user._user_pass = json.getString("user_pass");
			user._user_nicename = json.getString("user_nicename");
			user._user_email = json.getString("user_email");
			user._user_url = json.getString("user_url");
			user._user_registered = json.getString("user_registered");
			user._user_activation_key = json.getString("user_activation_key");
			user._user_status = json.getString("user_status");
			user._display_name = json.getString("display_name");
			user._user_code = json.getString("user_code");
			user._expired_code = json.getInt("expired_code");
		} catch (Exception e) {
			
		}
		
		return user;
	}
	
	public static User parseUserManager(JSONObject json) {
		if(json == null)
			return null;
		
		User user = null;
		try {
			user = new User();
			if(json.has("id"))
				user.setId(json.getString("id"));
			if(json.has("email_address"))
				user.setEmail(json.getString("email_address"));
			if(json.has("code")&&!json.isNull("code"))
				user.setCode(json.getString("code"));
			if(json.has("status")&&!json.isNull("status"))
				user.setStatus(Integer.parseInt(json.getString("status")));
			user.setType(Integer.parseInt(json.getString("type")));
			if(json.has("username"))
				user.setUserName(json.getString("username"));
			if(json.has("skype_name"))
				user.setSkypeName(json.getString("skype_name"));
			if(json.has("call_total"))
				user.setCallTotal(Integer.parseInt(json.getString("call_total")));
			if(json.has("user_code"))
				user.setUserCode(json.getString("user_code"));
			if(json.has("deleted"))
				user.setDeleted(Integer.parseInt(json.getString("deleted")));
			
		} catch (Exception e) {
			MyLog.d(GlobalConstant.TAG, "exception parseUserManager: "+e.getMessage());
		}
		
		return user;
	}
	
	public static Doctor parseDoctors(JSONObject json) {
		if(json == null)
			return null;
		
		Doctor doctor = null;
		try {
			doctor = new Doctor();
			doctor.setDoctorId(json.getString("id"));
			doctor.setDoctorName(json.getString("name"));
			doctor.setDoctorSkype(json.getString("skype"));
			doctor.setDoctorPriority(Integer.parseInt(json.getString("priority")));
			doctor.setDoctorStatus(Integer.parseInt(json.getString("status")));
		} catch (Exception e) {
			MyLog.d(GlobalConstant.TAG, "exception parseUserManager: "+e.getMessage());
		}
		
		return doctor;
	}
}
